package com.example.producerdemo.common;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author xzc
 */
@Component
public class RocketMqUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(RocketMqUtils.class);
    @Autowired
    private DefaultMQProducer producer;

    /**
     * 单个发送
     * @param message
     */
    public void send(Message message) {
        try {
            SendResult result = producer.send(message);
            SendStatus status = result.getSendStatus();
            LOGGER.info("messageId=" + result.getMsgId() + ", status=" + status);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
